package cn.store.web.servlet;

import cn.store.domain.Category;
import cn.store.domain.PageModel;
import cn.store.domain.Product;
import cn.store.domain.User;
import cn.store.service.CategoryService;
import cn.store.service.ProductService;
import cn.store.service.UserService;
import cn.store.service.serviceImp.CategoryServiceImp;
import cn.store.service.serviceImp.ProductServiceImp;
import cn.store.service.serviceImp.UserServiceImp;
import cn.store.utils.MyBeanUtils;
import cn.store.utils.UUIDUtils;
import cn.store.utils.UploadUtils;
import cn.store.web.base.BaseServlet;
import com.alibaba.fastjson.JSON;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdminUserManagerServlet extends BaseServlet {


	/**
	 * 查询用户带分页
	 * */
	public String findUsersWithPage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		//获取当前页
		String num = req.getParameter("num");
		int curNum = 1;
		if(num != null){
			curNum = Integer.parseInt(num);
		}
		//获取查询参数
		String username = req.getParameter("username");
		String name = req.getParameter("name");
		String email = req.getParameter("email");
		String telephone = req.getParameter("telephone");
		String state = req.getParameter("state");
		//封装搜索词
		Map<String,Object> searchWords = new HashMap<>();
		if(username == null || "".equals(username.trim())){
			username =null;
		}
		if(name == null || "".equals(name.trim())){
			name =null;
		}
		if(email == null || "".equals(email.trim())){
			email =null;
		}
		if(telephone == null || "".equals(telephone.trim())){
			telephone =null;
		}
		if(state == null || "-1".equals(state)) {
			searchWords.put("state", null);
		}else{
			searchWords.put("state", state);
		}
		searchWords.put("username",username);
		searchWords.put("name",name);
		searchWords.put("email",email);
		searchWords.put("telephone",telephone);
		//调用业务层查全部商品信息返回PageModel
		PageModel pm = null;
		UserService userService = new UserServiceImp();
		pm = userService.findUsersWithPage(curNum,searchWords);

		pm.setUrl("AdminUserManagerServlet?method=findUsersWithPage");
		//将PageModel放入request
		req.setAttribute("page", pm);

		//包装回搜索词,防止页面跳转丢失
		Product searchProduct = new Product();
		req.setAttribute("searchUser", searchWords);
		//转发到/admin/product/list.jsp
		return "/admin/user/list.jsp";
	}
	/**
	 *
	 * 通过Id查询用户	 * */
	public String findUserById(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
		String id = req.getParameter("uid");
		UserService userService = new UserServiceImp();
		User user = userService.findUserById(id);
		req.setAttribute("user",user);
		return "/admin/user/edit.jsp";
	}
	/**
	 * 修改用户
	 * */
	public String updateUser(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		User user = new User();
		MyBeanUtils.populate(user,req.getParameterMap());
		UserService userService = new UserServiceImp();
		User oldUser;
		oldUser = userService.findUserById(user.getUid());
		user.setPassword(oldUser.getPassword());
		user.setCode(oldUser.getCode());
		//System.out.println("更新");
		//System.out.println(user.toString());
		userService.updateUser(user);
		resp.sendRedirect(req.getContextPath() + "/AdminUserManagerServlet?method=findUsersWithPage");
		return null;
	}
	/**
	 * 通过id刪除用户
	 * */
	public String deleteUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String uid = req.getParameter("uid");
		Map<String,String> msg = new HashMap<>();
		resp.setContentType("application/json; charset=utf-8");
		PrintWriter p =resp.getWriter();
		try {
			/*尝试删除*/
			UserService userService = new UserServiceImp();
			int i = userService.deleteUser(uid);
			if(i > 0){
				msg.put("flag","true");
			}
			else{
				msg.put("flag","false");
			}
		}catch (Exception e){
			/*删除失败*/
			msg.put("flag","false");
			e.printStackTrace();
		}finally {
			String jsonStr = JSON.toJSONString(msg);
			System.out.println(jsonStr);
			p.write(jsonStr);
			return null;
		}

	}

}
